/*
 * Lou Decroo
 * Lab 6 & 8/ Modules 8 and 9
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LoginGUI extends JPanel implements ActionListener 
{
	private JPanel fieldsPanel, row1_id, row2_pass, row3, row4_buttons;
	private JButton submitButton;
	private JTextField userId;
	private JPasswordField pass;
	private JLabel	idLabel, passLabel;
	
	

	public LoginGUI()
	{
		fieldsPanel = new JPanel();

		fieldsPanel.setLayout(new BoxLayout(fieldsPanel,BoxLayout.Y_AXIS));

		row1_id = new JPanel(new FlowLayout());
		row2_pass = new JPanel(new FlowLayout());
		row3 = new JPanel(new FlowLayout());
		row4_buttons = new JPanel(new FlowLayout());

		


		
		
		submitButton = new JButton("SUBMIT");
		userId = new JTextField();
		userId.setPreferredSize(new Dimension(100,20));
		pass = new JPasswordField();
		pass.setEchoChar('*');
		pass.setPreferredSize(new Dimension(100,20));

		idLabel = new JLabel("Login ID");
		passLabel = new JLabel("Password");
		
		row1_id.add(idLabel);
		row1_id.add(userId);
		row2_pass.add(passLabel);
		row2_pass.add(pass);




		
		row4_buttons.add(submitButton);

		// add action listeners to each of the buttons
		submitButton.addActionListener(this);
		
		fieldsPanel.add(row1_id);
		fieldsPanel.add(row2_pass);
		fieldsPanel.add(row3);
		fieldsPanel.add(row4_buttons);
		fieldsPanel.setVisible(true);
		this.add(fieldsPanel, BorderLayout.NORTH);


		
	}

	public void actionPerformed( ActionEvent event){
		
		
		SystemController sc;
		// code for handling clear button
		if(event.getSource() == submitButton){
			//amm - utilizes SystemController
			sc = GUIMaster.getWorkhorse();
			if(sc.verifyLogin(userId.getText(), pass.getText()))
			{
				System.out.println("Logging in");	
				GUIMaster gm = GUIMaster.getInstance();
				if (sc.adminStatus == 1)
					gm.adminLogin();
				else
					gm.successfulLogin();
			}
			else{
				JOptionPane.showMessageDialog(null, "Invalid username or password! \nTry Again");

			}
		

		}
	}


	public static void main(String arg[]){
		JFrame frame = new JFrame();
		frame.add(new LoginGUI());
		frame.setSize(500, 500);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
}
